package com.action;

import java.io.Serializable;
import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;

import com.entity.Department;
import com.entity.Pagination;
import com.entity.Person;
import com.opensymphony.xwork2.ActionSupport;
import com.service.DepartmentService;
import com.service.PersonService;

public class ShowAction extends BaseAction implements Serializable {
	@Autowired
	private PersonService personService;
	@Autowired
	private DepartmentService departmentService;
	private List<Person> persons;
	List<Department> departments;
	private String type;
	private Pagination page;

	private int pageNum = 1;// dwz分页使用
	private int numPerPage = 20;
	private Department department;
	public Department getDepartment() {
		return department;
	}


	public void setDepartment(Department department) {
		this.department = department;
	}
	public int getPageNum() {
		return pageNum;
	}

	public int getNumPerPage() {
		return numPerPage;
	}

	public void setNumPerPage(int numPerPage) {
		this.numPerPage = numPerPage;
	}

	public void setPageNum(int pageNum) {
		this.pageNum = pageNum;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public List<Department> getDepartments() {
		return departments;
	}

	public void setDepartments(List<Department> departments) {
		this.departments = departments;
	}

	public List<Person> getPersons() {
		return persons;
	}

	public void setPersons(List<Person> persons) {
		this.persons = persons;
	}

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Person person;

	public Person getPerson() {
		return person;
	}

	public void setPerson(Person person) {
		this.person = person;
	}

	 @Action(value = "show-dep", results = { @Result(name = "success", location = "show-dep.jsp") }) 
	public String showDep() throws Exception {
		page = new Pagination(pageNum, numPerPage);
		if ("d".equalsIgnoreCase(type)) {
			int count = departmentService.countAll(department);
			page.setCount(count);	
			departments = departmentService.getAllDepartments(page,department);				
			
		}
		return "success";
	}

	@Override
	public String execute() throws Exception {
		page = new Pagination(pageNum, numPerPage);
		if ("p".equalsIgnoreCase(type)) {
			int count = personService.countAll(person);
			page.setCount(count);
			persons = personService.getAllPersons(page, person);

		} else if ("d".equalsIgnoreCase(type)) {
			int count = departmentService.countAll(department);
			page.setCount(count);
			departments = departmentService.getAllDepartments(page, department);
		}
		return "success";
	}

	public Pagination getPage() {
		return page;
	}

	public void setPage(Pagination page) {
		this.page = page;
	}
}
